<template>
  <div :style="bg">
    <div style="width: 100%; height: 100vh; overflow: hidden">
      <div style="width: 400px; margin: 100px auto">
<!--        font-size: 30px; text-align: center; padding: 30px 0-->
        <div style="color: #fafafa;
letter-spacing: 0;font-size: 40px; text-align: center; padding: 30px 0;
text-shadow: 0px 1px 0px #999, 0px 2px 0px #888, 0px 3px 0px #777, 0px 4px 0px #666, 0px 5px 0px #555, 0px 6px 0px #444, 0px 7px 0px #333, 0px 8px 7px #001135">欢迎注册</div>
        <el-form ref="form" :model="form" size="normal" :rules="rules">

          <el-form-item prop="username" >
            <el-input prefix-icon="el-icon-user-solid" v-model="form.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
            <el-form-item prop="nickName" >
                <el-input prefix-icon="el-icon-lightning" v-model="form.nickName" placeholder="请输入昵称" ></el-input>
            </el-form-item>
            <el-form-item prop="age" >
                <el-input prefix-icon="el-icon-sugar" v-model="form.age" placeholder="请输入年龄" ></el-input>
            </el-form-item>
            <el-form-item  prop="sex"  >
                <el-radio v-model="form.sex" label="男">男</el-radio>
                <el-radio v-model="form.sex" label="女">女</el-radio>
                <el-radio v-model="form.sex" label="未知">未知</el-radio>
            </el-form-item>
            <el-form-item  prop="address">
                <el-input type="textarea" v-model="form.address" style="width: 80%" placeholder="请输入地址"></el-input>
            </el-form-item>
          <el-form-item prop="password" >
            <el-input prefix-icon="el-icon-lock" v-model="form.password" show-password placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item prop="confirm" >
            <el-input prefix-icon="el-icon-lock" v-model="form.confirm" show-password placeholder="请再次输入密码"></el-input>
          </el-form-item>
          <el-form-item prop="phone" >
            <el-input prefix-icon="el-icon-lock" v-model="form.phone"  placeholder="请输入手机号"></el-input>
          </el-form-item>
          <el-form-item prop="code" >
            <el-input prefix-icon="el-icon-lock" v-model="form.code"  placeholder="请输入验证码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 100%" type="primary" @click="getCode">获取验证码</el-button>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 100%" type="primary" @click="register">注册</el-button>
          </el-form-item>
          
          <el-form-item><el-button type="text" @click="$router.push('/login')">&lt;&lt;返回登录 </el-button></el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Register",
  data() {
    return {
      bg: {
        backgroundImage:'url(' + require('../assets/background.jpg') + ')',
        backgroundRepeat: "no-repeat",
        backgroundSize: "100% 100%"
      },
      form: {},
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        confirm: [
          {required: true, message: '请确认密码', trigger: 'blur'},
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
        ]
      }
    }
  },
  methods: {
    getCode(){
      if(this.form.phone == null){
         this.$message({
          type: "error",
          message: '请输入手机号！'
        })
        return
      }
      request.post("/user/code",this.form).then(res => {
        console.log(res);
        if (res) {
       this.$message.success("验证码已发送")
        }
      })
    },
    register() {

      if (this.form.password !== this.form.confirm) {
        this.$message({
          type: "error",
          message: '2次密码输入不一致！'
        })
        return
      }

      this.$refs['form'].validate((valid) => {
        if (valid) {
          request.post("/user/register", this.form).then(res => {
            if (res.code === '0') {
              this.$message({
                type: "success",
                message: "注册成功"
              })
              this.$router.push("/login")  //登录成功之后进行页面的跳转，跳转到主页
            } else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
